﻿using Comm;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Kinlo.UI.Main.View
{
    /// <summary>
    /// GroupNameView.xaml 的交互逻辑
    /// </summary>
    public partial class GroupNameView : Window
    {
        public GroupNameView()
        {
            InitializeComponent();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(txtBatchNo.Text.Trim()))
            {
                MessageBox.Show("组别不能空，请重新录入", "错误");
                return;
            }
            else
            {
                try
                {
                    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                    if (config.AppSettings.Settings.AllKeys.Contains("GroupName"))
                    {
                        config.AppSettings.Settings["GroupName"].Value = txtBatchNo.Text.Trim();
                    }
                    else
                    {
                        config.AppSettings.Settings.Add("GroupName", txtBatchNo.Text.Trim());
                    }
                    config.Save(ConfigurationSaveMode.Modified);
                    ConfigurationManager.RefreshSection("appSettings");
                    GlobalValues.GroupName = txtBatchNo.Text.Trim();
                    Messenger.Default.Send<string>("", "UpdateUIGloableValues");
                    MessageBox.Show("录入成功 ", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    this.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(String.Format("录入失败 {0}", ex.Message), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }

        }
    }
}
